package com.lg.cms.controller.admin;



import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.cms.entity.Comment;
import com.lg.cms.entity.User;
import com.lg.cms.mapper.CommentMapper;
import com.lg.cms.mapper.UserMapper;
import com.lg.cms.service.CommentService;
import com.lg.cms.service.UserService;
import com.lg.cms.utils.Md5Util;
import com.lg.cms.vo.LayerTableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * @Description:会员管理控制器
 * @create: 2020-09-22 10:47
 */
@Controller
@Slf4j
@RequestMapping("/admin/users")
public class UserManageController {

    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    CommentMapper commentMapper;


    /**
     * 会员页面
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/members")
    public String members(Model model, HttpServletRequest request){
        model.addAttribute("request", request);
        return "/admin/interaction_management/membersCategory";

    }
    /**
     * 分页查询用户
     * @param pag
     * @param limit
     * @return
     */
    @ResponseBody
    @GetMapping("/showUsers")
    public LayerTableVO showUsers(@RequestParam("page")Integer pag, @RequestParam("limit")Integer limit,String userName){
        Page<User> page=new Page<>(pag,limit);


        LayerTableVO<User> layerData=userService.selectUserPage(page,userName);

        if (layerData.getCode()==0)
            log.info("分页查找数据成功");
        else
            log.info("分页查找数据为空");

        return layerData;
    }

    /**
     * id查找用户
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/selectUserIb")
    public User selectUserIb( @RequestParam("id")Long id){

        User layerData=userService.getById(id);
        layerData.setStringId(layerData.getId()+"");


        return layerData;
    }



    /**
     * 添加用户
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/addUser")
    public R addUser(User user){

        int n = userService.addUser(user);

        R r=new R();

        if (n>0){
            r.setCode(200);
            r.setMsg("添加成功");
        }else {
            r.setCode(304);
            r.setMsg("用户已存在");
        }

        return r;
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/updateUser")
    public R updateUser(User user){
        if (!StringUtils.isEmpty(user.getPassword()))
            user.setPassword(Md5Util.textToMd5(user.getPassword()));
        else user.setPassword(null);
        int n=userService.updateUser(user);

        R r=new R();

        if (n>0){
            r.setCode(200);
            r.setMsg("修改成功");
        }else {
            r.setCode(304);
            r.setMsg("修改失败");
        }

        return r;
    }

    /**
     * 根据id批量删除用户
     * @param ids
     * @return
     */
    @ResponseBody
    @GetMapping("/deleteUsersByIds")
    public R deleteUsersByIds(@RequestParam("ids") String ids){
        int n=userService.deleteUsersByIds(ids);

        R r=new R();

        if (n>0){
            r.setCode(200);
            r.setMsg("删除成功");
        }else {
            r.setCode(304);
            r.setMsg("删除失败");
        }

        return r;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ResponseBody
    @GetMapping("/deleteUserById")
    public R deleteUserById(@RequestParam("id") Long id){

        boolean tf=false;
        int n=commentMapper.delete(Wrappers.<Comment>lambdaQuery().eq(Comment::getUserId,id));
        if (n!=0) tf=userService.removeById(id);
        R r=new R();

        if (tf){
            r.setCode(200);
            r.setMsg("删除成功");
        }else {
            r.setCode(304);
            r.setMsg("删除失败");
        }

        return r;
    }


}
